package test.core.db;

import java.util.ArrayList;
import java.util.List;


import com.alibaba.fastjson.JSON;

import hyl.core.MyDate;
import hyl.core.db.DBPool;
import hyl.core.db.Htable;
import hyl.core.db.MyDB;
import hyl.core.reflect.MyKryo;

public class dbtest {
	DBPool dbpool = DBPool.getInstance();
	public static String DB1 = "db1";
	public static String DB2 = "db2";
	MyDB _db1;
	MyDB _db2;
	public dbtest() {
	
	}
	public void conn() {
		_db2=	MyDB.createDB(DB1);
		
		//_db2=	MyDB.createCommitDB(DB1);
	}
	public static void main(String[] args) {
		dbtest t=new dbtest();
		t.conn();
		while(true) {	//并且测试一下断线重连是否可行
		
			//t.conn();
			t.序列化();
			MyDate.sleeps(4);
		}
		//t.test1() ;

	}
	public void test1() {
		_db2.executeUpdate("drop table if exists test", null);
		_db2.executeUpdate("create table test(id int,name varchar(50),tx varchar(50))", null);
		ArrayList<String> al = new ArrayList<String>();
		al.add("insert into test(id,name,tx)values(1,'ddff1f','dddd')");
		al.add("insert into test(id,name,tx)values(2,'ddff2f','dddd','vv')");
		al.add("insert into test(id,name,tx)values(3,'ddff3f','dddd')");
		_db2.batch(al);
		System.out.println("test1.插入1");
		List<Object[]> list = new ArrayList<>();
		list.add(new Object[] { 4,"tyyy", "hhhh" });
		list.add(new Object[] { "yyyy", "hhhh", "ddd" });
		list.add(new Object[] { 5,"yyyytt", "hhhh" });
		int n = _db2.batch("insert into test (id,name,tx) values(?,?,?)", list);
		System.out.println("test1.插入2");
		_db2.commit();
		Htable ht=	_db2.queryHtable("Select * From test", null);
		System.out.println(JSON.toJSONString(ht));
		System.out.println(JSON.toJSONString(ht.cols));
		System.out.println(JSON.toJSONString(ht.rows));
	}

	public void 序列化() {
		Htable ht=	_db2.queryHtable("Select * From test", null);
		MyKryo kyo=MyKryo.getInstance();
		byte[] bb=kyo.serialize(ht);
		Htable ht2=	kyo.deserialize(bb);
		System.out.println(JSON.toJSONString(ht2));
		
	}
}
